import Validator from '@/utils/Validator'
import router from '@/router'
/**
 * 路由跳转 -> 只执行跳转，拦截还是由守卫完成
 * @param {*} params 
 * @returns 
 */
function navigateTo(params) {
    let url = params?.url
    const queryParams = {};
    const queryString = url.split('?')[1];
    if (queryString) {
        const keyValuePairs = queryString.split('&');
        for (const pair of keyValuePairs) {
            const [key, value] = pair.split('=');
            queryParams[key] = decodeURIComponent(value);
        }
    }
    return new Promise(function (resolve, reject) {
        if (Validator.isEmpty(url)) {
            reject('path is empty')
        }
        router.push({
            path:url,
            params:queryParams
        })
        resolve(url)
    })
}
function navigateBack() {
    return new Promise(function (resolve, reject) {
        router.go(-1)
        resolve(true)
    })
}
function redirectTo(params) {
    let url = params?.url
  
    return new Promise(function (resolve, reject) {
        if (Validator.isEmpty(url)) {
            reject('path is empty')
        }
        router.push(url)
        resolve(url)
    })
}
export default {
    navigateTo,
    navigateBack,
    redirectTo
}